#include <odb/database.hxx>
#include <odb/mysql/database.hxx>
#include <gflags/gflags.h>
#include <butil/logging.h>
#include "../mytest/user.hxx"
#include "../mytest/user-odb.hxx"
#include "logger.hpp"   // 日志模块封装



namespace bite_im{
    using UserQuery = odb::query<User>;
    using UserResult = odb::result<User>;
    class UserTable {
        public:
            UserTable(const std::shared_ptr<odb::mysql::database> db):_db(db){}
            ~UserTable(){}
            // 新增用户
            bool insert_user(const std::shared_ptr<User> &user) {
                try {   
                    //获取事务对象开启事务
                    odb::transaction trans(_db->begin());
                    _db->persist(*user);
                    trans.commit();
                    return true;
                } catch (const odb::exception &e) {
                    LOG_ERROR("新增用户失败: {}",e.what());
                    return false;
                }
            }
            // 修改用户
            bool update_user(const std::shared_ptr<User> &user) {
                try {
                    //获取事务对象开启事务
                    odb::transaction trans(_db->begin());
                    _db->update(*user);
                    trans.commit();
                    return true;
                } catch (const odb::exception &e) {
                    LOG_ERROR("修改用户失败: {}",e.what());
                    return false;
                }
            }
            // 通过昵称查询用户
            std::shared_ptr<User> select_by_nickname(const std::string &nickname) {
                std::shared_ptr<User> res;
                try {
                    //获取事务对象开启事务
                    odb::transaction trans(_db->begin());
                    UserResult r(_db->query<User>(UserQuery::nickname == nickname));
                    if (r.empty()) {
                        return nullptr;
                    }
                    res = std::make_shared<User>(*r.begin());
                    trans.commit();
                } catch (const odb::exception &e) {
                    LOG_ERROR("通过昵称查询用户失败: {}",e.what());
                }
                return res;
            }
            
            // 通过手机号查询用户
            std::shared_ptr<User> select_by_phone(const std::string &phone) {   
                std::shared_ptr<User> res;
                try {
                    //获取事务对象开启事务
                    odb::transaction trans(_db->begin());
                    UserResult r(_db->query<User>(UserQuery::phone == phone));
                    if (r.empty()) {
                        return nullptr;
                    }
                    res = std::make_shared<User>(*r.begin());
                    trans.commit();
                } catch (const odb::exception &e) {
                    LOG_ERROR("通过手机号查询用户失败: {}",e.what());
                }
                return res;
            }

            // 通过用户ID查询用户
            std::shared_ptr<User> select_by_id(const std::string &user_id) {
                std::shared_ptr<User> res;
                try {
                    //获取事务对象开启事务
                    odb::transaction trans(_db->begin());
                    UserResult r(_db->query<User>(UserQuery::user_id == user_id));
                    if (r.empty()) {
                        return nullptr;
                    }
                    res = std::make_shared<User>(*r.begin());
                    trans.commit();
                } catch (const odb::exception &e) {
                    LOG_ERROR("通过用户ID查询用户失败: {}",e.what());
                }
                return res;
            }
            //通过多名用户ID查询用户
            std::vector<std::shared_ptr<User>> select_by_ids(const std::vector<std::string> &user_ids) {
                std::vector<std::shared_ptr<User>> res;
                try {
                    //获取事务对象开启事务  
                    odb::transaction trans(_db->begin());
                    for (const auto &user_id : user_ids) {
                        UserResult r(_db->query<User>(UserQuery::user_id == user_id));
                        if (!r.empty()) {
                            res.push_back(std::make_shared<User>(*r.begin()));
                        }
                    }
                    trans.commit();
                } catch (const odb::exception &e) {
                    LOG_ERROR("通过多名用户ID查询用户失败: {}",e.what());
                }
                return res;
            }

        private:
            std::shared_ptr<odb::mysql::database> _db;
    };
}
